Often times software is created that needs to communicate with other software when certain events occur. For example, in a computer windowing system, when a user selects a window on the display, the window system needs to notify the software that is drawing information in the window that the window has been selected. In prior systems, the software needing notification of certain events registers the events for which it wants to be notified with the software that raises the events. In some prior systems, as part of the registration mechanism, the software needing notification registers a notification function by which it can be notified. Then, when the software raises an event that was previously registered, the registered notification function is called. This is known in the prior art as a callback mechanism.
An overview of well-known object-oriented programming techniques is provided, since the present invention is described below using object-oriented concepts. Two common characteristics of object-oriented programming languages are support for data encapsulation and data type inheritance. Data encapsulation refers to the binding of functions and data. Inheritance refers to the ability to declare a data type in terms of other data types.
In the C++ language, object-oriented techniques are supported through the use of classes. A class is a user-defined type. A class declaration describes the data members and function members of the class. For example, the following declaration defines data members and a function member of a class named CIRCLE.
______________________________________ class CIRCLE {public: int x, y; int radius; void draw(); }; ______________________________________
Variables x and y specify the center location of a circle and variable radius specifies the radius of the circle. These variables are referred to as data members of the class CIRCLE. The function draw is a user-defined function that draws the circle of the specified radius at the specified location. The: function draw is referred to as a function member of class CIRCLE. A function member is also referred to as a method of a class. The data members and function members of a class are bound together in that the function operates on an instance of the class. An instance of a class is also called an object of the class.
In the syntax of C++, the following statement declares the objects a and b to be of type class CIRCLE.
CIRCLE a, b; PA1 a.x=2; PA1 a.y=2; PA1 a.radius=1: PA1 b.x=4; PA1 b.y=5; PA1 b.radius=2; PA1 a.draw( ); PA1 b.draw( ); PA1 class CIRCLE.sub.-- 1: CIRCLE { . . . }; PA1 class CIRCLE.sub.-- 2: CIRCLE { . . . }; PA1 class PATTERN: CIRCLE.sub.-- 1, CIRCLE.sub.-- 2{ . . . }; PA1 class CIRCLE.sub.-- 1: virtual CIRCLE { . . . }; PA1 class CIRCLE.sub.-- 2: virtual CIRCLE { . . . }; PA1 class PATTERN: CIRCLE.sub.-- 1, CIRCLE.sub.-- 2{ . . . }; PA1 CIRCLE *c.sub.-- ptr; PA1 c.sub.-- ptr=&c; PA1 CIRCLE a: PA1 CIRCLE.sub.-- FILL b; PA1 a. draw( ); PA1 b.draw( ); PA1 CIRCLE *c.sub.-- ptr; PA1 c.sub.-- ptr=&b; PA1 c.sub.-- ptr-&gt;draw( ); // CIRCLE.sub.-- FILL::draw( ) PA1 void XX::AddRef( ) {refcount++}; PA1 void XX::Release( ) {if (--refcount==0) delete this;} PA1 if (pSpreadsheet-&gt;QueryInterface("IDatabase", & plDatabase)) PA1 // IDatabase supported PA1 else PA1 //IDatabase not supported
This declaration causes the allocation of memory for the objects a and b. The following statements assign data to the data members of objects a and b.
The following statements are used to draw the circles defined by objects a and b.
A derived class is a class that inherits the characteristics--data members and function members--of its base classes. For example, the following derived class CIRCLE.sub.-- FILL inherits the characteristics of the base class CIRCLE.
______________________________________ class CIRCLE.sub.-- FILL: CIRCLE {public: int pattern; void fill(); }; ______________________________________
This declaration specifies that class CIRCLE.sub.-- FILL includes all the data and function members that are in class CIRCLE in addition to those data and function members introduced in the declaration of class CIRCLE.sub.-- FILL, that is, data member pattern and function member fill. In this example, class CIRCLE.sub.-- FILL has data members x, y, radius, and pattern and function members draw and fill. Class CIRCLE.sub.-- FILL is said to "inherit" the characteristics of class CIRCLE. A class that inherits the characteristics of another class is a derived class (e.g., CIRCLE.sub.-- FILL). A class that does not inherit the characteristics of another class is a primary (root) class (e.g., CIRCLE). A class whose characteristics are inherited by another class is a base class (e.g., CIRCLE is a base class of CIRCLE.sub.-- FILL). A derived class may inherit the characteristics of several classes, that is, a derived class may have several base classes. This is referred to as multiple inheritance.
A derived class may specify that a base class is to be inherited virtually. Virtual inheritance of a base class means that only one instance of the virtual base class exists in the derived class. For example, the following is an example of a derived class with two nonvirtual base classes.
In this declaration class PATFERN inherits class CIRCLE twice nonvirtually through classes CIRCLE.sub.-- 1 and CIRCLE.sub.-- 2. There are two instances of class CIRCLE in class PATTERN.
The following is an example of a derived class with two virtual base classes.
The derived class PATTERN inherits class CIRCLE twice virtually through classes CIRCLE.sub.-- 1 and CIRCLE.sub.-- 2. Since the class CIRCLE is virtually inherited twice, there is only one object of class CIRCLE in the derived class PATTERN. One skilled in the art would appreciate virtual inheritance can be very useful when the class derivation is more complex.
A class may also specify whether its function members are virtual. Declaring that a function member is virtual means that the function can be overridden by a function of the same name and type in a derived class. In the following example, the function draw is declared to be virtual in classes CIRCLE and CIRCLE.sub.-- FILL.
______________________________________ class CIRCLE {public; int x, y; int radius; virtual void draw(); }; class CIRCLE.sub.-- FILL: CIRCLE {public: int pattern; virtual void draw(); }; ______________________________________
If a virtual function is declared without providing an implementation, then it is referred to as a pure virtual function. A pure virtual function is a virtual function declared with the pure specifier, "=O". If a class specifies a pure virtual function, then any derived class needs to specify an implementation for that function member before that function member may be invoked.
In order to access objects, the C++ language provides a pointer data type. A pointer holds values that are addresses of objects in memory. Through a pointer, an object can be referenced. The following statement declares variable c.sub.-- ptr to be a pointer on an object of type class CIRCLE and sets variable c.sub.-- ptr to hold the address of object c.
Continuing with the example, the following statement declares object a to be of type class CIRCLE and object b to be of type class CIRCLE.sub.-- FILL.
The following statement refers to the function draw as defined in class CIRCLE.
Whereas, the following statement refers to the function draw defined in class CIRCLE.sub.-- FILL.
Moreover, the following statements type cast object b to an object of type class CIRCLE and invoke the function draw that is defined in class CIRCLE.sub.-- FILL.
Thus, the virtual function that is called is function CIRCLE.sub.-- FILL::draw.
FIG. 1 is a block diagram illustrating typical data structures used to represent an object. An object is composed of instance data (data members) and member functions, which implement the behavior of the object. The data structures used to represent an object comprise instance data structure 101, virtual function table 102, and the function members 103, 104, 105. The instance data structure 101 contains a pointer to the virtual function table 102 and contains data members. The virtual function table 102 contains an entry for each virtual function member defined for the object. Each entry contains a reference to the code that implements the corresponding function member. The layout of this sample object confirms to the model defined in U.S. patent application Ser. No. 07/682,537, entitled "A Method for Implementing Virtual Functions and Virtual Bases in a Compiler for an Object Oriented Programming Language," which is hereby incorporated by reference. In the following, an object will be described as an instance of a class as defined by the C++ programming language. One skilled in the art would appreciate that objects can be defined using other programming languages.
An advantage of using object-oriented techniques is that these techniques can be used to facilitate the sharing of objects. In particular, object-oriented techniques facilitate the creation of compound documents. A compound document is a document that contains objects generated by various computer programs. (Typically, only the data members of the object and the class type are stored in a compound document.) For example, a word processing document that contains a spreadsheet object generated by a spreadsheet program is a compound document. A word processing program allows a user to embed a spreadsheet object (e.g., a cell) within a word processing document. To allow this embedding, the word processing program is compiled using the class definition of the object to be embedded to access function members of the embedded object. Thus, the word processing program would need to be compiled using the class definition of each class of objects that can be embedded in a word processing document. To embed an object of a new class into a word processing document, the word processing program would need to be recompiled with the new class definition. Thus, only objects of classes selected by the developer of the word processing program can be embedded. Furthermore, new classes can only be supported with a new release of the word processing program.
To allow objects of an arbitrary class to be embedded into compound documents, interfaces are defined through which an object can be accessed without the need for the word processing program to have access to the class definitions at compile time. An abstract class is a class in which there is at least one virtual function member with no implementation (a pure virtual function member). An interface is an abstract class with no data members and whose virtual functions are all pure. Thus, an interface provides a protocol for two programs to communicate. Interfaces are typically used for derivation: a program implements classes that provide implementations for the interfaces the classes are derived from. Thereafter, objects are created as instances of these derived classes.
The following class definition is an example definition of an interface. In this example, for simplicity of explanation, rather than allowing any class of object to be embedded in its documents, a word processing program allows spreadsheet objects to be embedded. Any spreadsheet object that provides this interface can be embedded, regardless of how the object is implemented. Moreover, any spreadsheet object, whether implemented before or after the word processing program is compiled, can be embedded.
__________________________________________________________________________ class ISpreadSheet { virtual void File() = 0; virtual void Edit() = 0; virtual void Formula() = 0; virtual void Format() = 0; virtual void GetCell (string RC, cell *pCell) = 0; virtual void Data() = 0; } __________________________________________________________________________
The developer of a spreadsheet program would need to provide an implementation of the interface to allow the spreadsheet objects to be embedded in a word processing document.
When the word processing program embeds a spreadsheet object, the program needs access to the code that implements the interface for the spreadsheet object. To access the class code, each implementation is given a unique class identifier. For example, code implementing a spreadsheet object developed by Microsoft Corporation may have a class identifier of "MSSpreadsheet," while code implementing a spreadsheet object developed by another corporation may have a class identifier of "LTSSpreadsheet." A persistent registry in each computer system is maintained that maps each class identifier to the code that implements the class. Typically, when a spreadsheet program is installed on a computer system, the persistent registry is updated to reflect the availability of that class of spreadsheet objects. So long as a spreadsheet developer implements each function member defined by the interface and the persistent registry is maintained, the word processing program can embed instances of the developer's spreadsheet objects into a word processing document. The word processing program accesses the functionk members of the embedded spreadsheet objects without regard to who has implemented them or how they have been implemented.
Various spreadsheet developers may wish, however, to implement only certain function members. For example, a spreadsheet developer may not want to implement database support, but may want to support all other function members. To allow a spreadsheet developer to support only some of the function members, while still allowing the objects to be embedded, multiple interfaces for spreadsheet objects are defined. For example, the, interfaces IDatabase and IBasic may be defined for a spreadsheet object as follows.
__________________________________________________________________________ class IBasic { virtual void File() = 0; virtual void Edit() = 0; virtual void Formula() = 0; virtual void Format() = 0; virtual void GetCell (string RC, cell *pCell) = 0; } class IDatabase { virtual void Data() = 0; } __________________________________________________________________________
Each spreadsheet developer would implement the IBasic interface and, optionally, the IDatabase interface.
At run time, the word processing program would need to determine whether a spreadsheet object to be embedded supports the IDatabase interface. To make this determination, another interface is defined (that every spreadsheet object implements) with a function member that indicates which interfaces are implemented for the object. This interface is named IUnknown (and referred to as the unknown interface or the object management interface) and is defined as follows.
______________________________________ class IUnknown { virtual HRESULT QueryInterface (REFIID iid, void **ppv) = 0; .sup. virtual ULONG AddRef() = 0; .sup. virtual ULONG Release () = 0; ______________________________________
The IUnknown interface defines the function member (method) QueryInterface. The method QueryInterface is passed an interface identifier (e.g., "IDatabase") in parameter iid (of type REFIID) and returns a pointer to the implementation of the identified interface for the object for which the method is invoked in parameter ppv. If the object does not support the interface, then the method returns a false. The type HRESULT indicates a predefined status, and the type ULONG indicates an unsigned long integer.
Code TABLE 1 ______________________________________ HRESULT XX::QueryInterface(REFIID iid, void **ppv) { ret = TRUE; switch (iid) { case IID.sub.-- IBasic; *ppv = *pIBasic; break; case IID.sub.-- IDatabase: *ppv = *pIDatabase; break; case IID.sub.-- IUnknown: *ppv = this; break; default: ret = FALSE; if (ret == TRUE) {AddRef();}; return ret; } ______________________________________
Code Table 1 contains pseudocode for C++ source code for a typical implementation of the method QueryInterface for class XX, which inherits the class IUnknown. If the spreadsheet object supports the IDatabase interface, then the method QueryInterface includes the appropriate case label within the switch statement. The variables plBasic and plDmabase point to a pointer to the virtual function tables of the IBasic and IDatabase interfaces, respectively. The method QueryInterface invokes to method AddRef (described below) to increment a reference count for the object of class XX when a pointer to an interface is returned.